import 'package:get/get.dart';
import 'package:tiktokadult/hj_model/video_model.dart';
import 'package:tiktokadult/hj_utils/api_service/tag_service.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

abstract class TagDetailSubController extends GetxController {
  List<VideoModel> dataSource = [];
  bool isGridStyle = true;
  double childRatio = 1;
  bool isLoading = true;
  String? loadType;
  String? newsType;
  String? sort;
  int? postSort;
  int page = 1;
  TagsBean tag;

  TagDetailSubController(this.tag, {this.postSort});

  RefreshController? controller;

  @override
  void onInit() {
    super.onInit();
  }

  @override
  void onReady() {
    super.onReady();
    // isLoading = false;
    fetchPageData(needLoading: true);
  }

  fetchPageData({bool isRefresh = true, bool needLoading = false}) async {
    if (isRefresh) {
      page = 1;
    }

    isLoading = needLoading;
    update();
    final res = await TagService.fetchTagList(
      tag.id ?? '',
      newsType: newsType,
      sortType: postSort,
      page: page,
      size: 10,
    );
    if (isRefresh) {
      dataSource.clear();
      controller?.refreshCompleted();
    }

    dataSource.addAll(res?.videos ?? []);
    page += 1;
    isLoading = false;
    update();
    if (res?.hasNext ?? false) {
      controller?.loadComplete();
    } else {
      controller?.loadNoData();
    }
  }
}

class TagVideoController extends TagDetailSubController {
  TagVideoController(super.tag);

  @override
  String get loadType => 'MOVIE';
  String get newsType => tag.newsType ?? "MOVIE";

  @override
  double get childRatio => 168 / 119;
}

class TagSPController extends TagDetailSubController {
  TagSPController(super.tag);
  @override
  String get loadType => 'SP';
  String get newsType => tag.newsType ?? "SP";

  @override
  double get childRatio => 168 / 248;
}

class TagCOVERController extends TagDetailSubController {
  TagCOVERController(super.tag, {super.postSort});

  @override
  String? get loadType => null;
  String get newsType => tag.newsType ?? "";

  @override
  bool get isGridStyle => false;
}

T instanceController<T>(TagsBean tag, {int? postSort}) {
  final type = T.toString();
  if (type == 'TagCOVERController') return TagCOVERController(tag, postSort: postSort) as T;
  if (type == 'TagSPController') return TagSPController(tag) as T;
  if (type == 'TagVideoController') return TagVideoController(tag) as T;
  throw '$type 没有对应类型';
}
